//
//  PageCollectionView.swift
//  AFNetworking
//
//  Created by admin on 2020/4/26.
//

import UIKit

import YYKit

public class PageCollectionView: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    //
    @objc public var selectItem:NSDictionary?
    
    // 元素
    var _items:NSArray=[]
    @objc public var items:NSArray{
        get{
            return _items
        }
        set{
            _items = newValue
            selectItem = _items.firstObject as? NSDictionary
            self .reloadData()
        }
    }

    public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: layout)
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = UIColor.white;
   
        self.register(PageCollectionViewCell.self, forCellWithReuseIdentifier: "PageCollectionViewCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}
extension PageCollectionView{
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count;
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:PageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PageCollectionViewCell", for: indexPath) as! PageCollectionViewCell
        let celldic:NSDictionary = items.object(at: indexPath.row) as! NSDictionary
        cell.isSelected = (celldic == selectItem)
    
        cell.titleLab.text = celldic .stringValue(forKey: "title", default: "")
        return cell
    }
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let celldic:NSDictionary = items.object(at: indexPath.row) as! NSDictionary
        selectItem = celldic
        collectionView .reloadData()
    }
  
}
